
#include <iostream>

#include "ringqueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

class ThreadDate
{
public:
    RingQueue<Task>* bq;
    std::string _name;
};

void* consumer(void* args)
{
    ThreadDate* td = static_cast<ThreadDate *> (args);
    while(true)
    {
        Task t;
        td->bq->Pop(&t);
        t();
        std::cout << "consumer:" << td->_name << " "<<t.PrintResult() << std::endl;
        
    }
    return nullptr;
}

void* producter(void *args)
{
    RingQueue<Task> *bq = static_cast<RingQueue<Task>*>(args);
    while(true)
    {
        int data_x = rand()%10;
        usleep(rand()%123);
        char oper = opers[rand()%opers.size()];
        usleep(rand()%123);
        int data_y = rand()%10;
        Task t(data_x,data_y,oper);


        std::cout << "product" <<  "task:" << t.PrintTask() << std::endl;
        bq->Push(t);
        sleep(1);

    }
    return nullptr;
}




int main()
{   
    srand(time(NULL)^getpid()^pthread_self());
    RingQueue<Task> * bq = new RingQueue<Task>();
    ThreadDate* td1 = new ThreadDate();
    td1->bq = bq;
    td1->_name = "comsumer-1";
    pthread_t c[3],p[2];
    pthread_create(&c[0],nullptr,consumer,td1);

    ThreadDate* td2 = new ThreadDate();
    td2->bq = bq;
    td2->_name = "comsumer-2";
    pthread_create(&c[1],nullptr,consumer,td2);

     ThreadDate* td3 = new ThreadDate();
    td3->bq = bq;
    td3->_name = "comsumer-3";

    pthread_create(&c[2],nullptr,consumer,td3);


    pthread_create(&p[0],nullptr,producter,bq);
    pthread_create(&p[1],nullptr,producter,bq);



    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);
    pthread_join(c[2],nullptr);
    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);

    return 0;
}